package com.cmbdb.core.common;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 注释
 *
 * @author: coding99
 * @Date: 17-3-3
 * @Time: 上午11:28
 */
public abstract class AutoController<T> extends BaseController{

    protected abstract BaseService getBaseService();


    protected abstract T getEntity(T t,String type);


    /**
     * 插入一条记录
     * @param object
     * @return
     */
    @RequestMapping(value = "save",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public AjaxJson save(T object) {

        AjaxJson ajaxJson = new AjaxJson();

        boolean result = getBaseService().insert(getEntity(object,"add"));

        if(!result) {
            ajaxJson.setMsg("保存失败！");
        }else{
            ajaxJson.setMsg("保存成功！");
        }

        ajaxJson.setSuccess(result);


        return ajaxJson;

    }


    /**
     * 插入一条记录（选择字段， null 字段不插入）
     * @param object
     * @return
     */
    @RequestMapping(value = "saveSelective",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public AjaxJson saveSelective(T object) {

        AjaxJson ajaxJson = new AjaxJson();

        boolean result = getBaseService().insertSelective(getEntity(object,"add"));
        if(!result) {
            ajaxJson.setMsg("保存失败！");
        }else{
            ajaxJson.setMsg("保存成功！");
        }

        ajaxJson.setSuccess(result);


        return ajaxJson;

    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @RequestMapping(value = "deleteById",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public AjaxJson deleteById(@RequestParam int id) {

        AjaxJson ajaxJson = new AjaxJson();
        boolean result = getBaseService().deleteByPrimaryKey(id);

        if(!result) {
            ajaxJson.setMsg("删除失败！");
        }else{
            ajaxJson.setMsg("删除成功！");
        }
        ajaxJson.setSuccess(result);

        return ajaxJson;

    }


    /**
     * 根据 ID 选择修改
     * @param object
     * @return
     */
    @RequestMapping(value = "updateById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public AjaxJson updateById(T object) {

        AjaxJson ajaxJson = new AjaxJson();

        boolean result = getBaseService().updateByPrimaryKey(getEntity(object,"edit"));

        if(!result) {
            ajaxJson.setMsg("修改失败！");
        }else{
            ajaxJson.setMsg("修改成功！");
        }
        ajaxJson.setSuccess(result);


        return ajaxJson;


    }


    /**
     * 根据 ID 选择修改(null字段不修改)
     * @param object
     * @return
     */
    @RequestMapping(value = "updateByIdSelective",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public AjaxJson updateByIdSelective(T object) {

        AjaxJson ajaxJson = new AjaxJson();

        boolean result = getBaseService().updateByPrimaryKeySelective(getEntity(object,"edit"));

        if(!result) {
            ajaxJson.setMsg("修改失败！");
        }else{
            ajaxJson.setMsg("修改成功！");
        }
        ajaxJson.setSuccess(result);

        return ajaxJson;


    }


    /**
     * 根据 ID 查询
     * @param id
     * @return
     */
    @RequestMapping(value = "queryById",method = RequestMethod.GET,produces = "application/json")
    public AjaxJson queryById(@RequestParam int id) {

        AjaxJson ajaxJson = new AjaxJson();
        T obj = (T)getBaseService().selectByPrimaryKey(id);
        ajaxJson.setObj(obj);
        return ajaxJson;

    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping(value = "queryAll",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public AjaxJson queryAll() {

        AjaxJson ajaxJson = new AjaxJson();
        List<T> list = getBaseService().selectAll();
        ajaxJson.setObj(list);

        return ajaxJson;

    }




}
